package org.zecontech.plantvszombie.datagen;

import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.BuiltInRegistries;
import org.zecontech.plantvszombie.PlantVsZombieMod;
import org.zecontech.plantvszombie.util.Helpers;

import java.util.concurrent.CompletableFuture;

// 模组英文语言文件生成器
public class ModLanguageProviderEn extends FabricLanguageProvider {

    protected ModLanguageProviderEn(FabricDataOutput dataOutput, CompletableFuture<HolderLookup.Provider> registryLookup) {
        super(dataOutput, "en_us", registryLookup);
    }

    @Override
    public void generateTranslations(HolderLookup.Provider provider, TranslationBuilder builder) {
        BuiltInRegistries.ITEM.forEach(item -> {
            if (BuiltInRegistries.ITEM.getKey(item).getNamespace().equals((PlantVsZombieMod.MOD_ID))) {
                builder.add(item, formatFieldName(Helpers.getItemName(item)));
            }
        });
        BuiltInRegistries.ENTITY_TYPE.forEach(entityType -> {
            if (BuiltInRegistries.ENTITY_TYPE.getKey(entityType).getNamespace().equals((PlantVsZombieMod.MOD_ID))) {
                builder.add(entityType, formatFieldName(Helpers.getEntityName(entityType)));
            }
        });
    }

    private static String formatFieldName(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }

        StringBuilder result = new StringBuilder();
        boolean nextUpper = true;

        for (char c : input.toCharArray()) {
            if (c == '_') {
                result.append(' ');
                nextUpper = true;
            } else {
                if (nextUpper) {
                    result.append(Character.toUpperCase(c));
                    nextUpper = false;
                } else {
                    result.append(Character.toLowerCase(c));
                }
            }
        }

        return result.toString();
    }
}
